package com.ctl.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ctl.dto.VideoDTO;
import com.ctl.entity.Category;
import com.ctl.entity.User;
import com.ctl.entity.Video;
import com.ctl.mapper.CategoryMapper;
import com.ctl.mapper.UserMapper;
import com.ctl.service.VideoService;
import com.ctl.mapper.VideoMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;

/**
* @author stars
* @description 针对表【video(视频)】的数据库操作Service实现
* @createDate 2023-02-22 16:53:38
*/
@Service
public class VideoServiceImpl extends ServiceImpl<VideoMapper, Video>
    implements VideoService{
    @Autowired
    private VideoMapper mapper;
//    @Autowired
//    private UserMapper userMapper;
//    @Autowired
//    private CategoryMapper categoryMapper;

    @Override
    public VideoDTO getByAll(Integer page, Integer pre_page, Integer id, String name,
                                   Integer category_id, String uploader_name) {
        List<Video> videos = mapper.getByAll((page - 1) * pre_page, pre_page,id, name, category_id, uploader_name);
        VideoDTO videoDTO = new VideoDTO();
        videoDTO.setTotal_count(videos.size());
        for (Video video : videos) {
            video.setCategory(video.getCategorys().getName());
        }
        videoDTO.setItems(videos);
//        QueryWrapper<Video> queryWrapper = new QueryWrapper<>();
//        List<User> users=new ArrayList<>();
//        Category category = null;
//        List<Video> videosList = new ArrayList<>();
//        if (id!=null){
//            queryWrapper.eq("id",id);
//        }
//        if (category_id!=null){
//            QueryWrapper<Category> wrapper = new QueryWrapper<>();
//            wrapper.eq("id",category_id);
//            category = categoryMapper.selectOne(wrapper);
//        }
//        if (StringUtils.hasText(uploader_name)){
//            QueryWrapper<User> wrapper = new QueryWrapper<>();
//            wrapper.like("name",uploader_name);
//            users = userMapper.selectList(wrapper);
//        }
//        Page<Video> videoPage = new Page<>(page,pre_page);
//        mapper.selectPage(videoPage,queryWrapper);
//        List<Video> videos = videoPage.getRecords();
//        System.out.println("category = " + category);
//        for (Video video : videos) {
//            System.out.println("video = " + videos);
//            if (!users.isEmpty()){
//                for (User user : users) {
//                    if (user.getId()==video.getUid()){
//                        video.setUploader(user);
//                        videosList.add(video);
//                    }
//                }
//            }
//            if (category!=null){
//                if (video.getCategoryId()==category.getId()){
//                    video.setCategory(category.getName());
//                    videosList.add(video);
//                }
//            }
//            if (category_id==null){
//                QueryWrapper<Category> wrapper = new QueryWrapper<>();
//                wrapper.eq("id",video.getCategoryId());
//                Category categorys = categoryMapper.selectOne(wrapper);
//                if (video.getCategoryId()==categorys.getId()){
//                    video.setCategory(category.getName());
//                    videosList.add(video);
//                }
//            }
//            QueryWrapper<User> userWrapper = new QueryWrapper<>();
//            userWrapper.eq("id",video.getId());
//            User user = userMapper.selectOne(userWrapper);
//            video.setUploader(user);
//        }
//        VideoDTO videoDTO = new VideoDTO();
//        videoDTO.setTotal_count(videosList.size());
//        videoDTO.setItems(videosList);
        return videoDTO;
    }
}




